package com.jackrain.nea.fc.dl.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.dl.api.ViewDLPurAdjPriceToF4QueryCmd;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ReferenceUtil;
import com.jackrain.nea.util.ValueHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;

/**
 * @author wuwenxue
 * @date 2018/05/22
 **/
@Api(description = "FC采购价格调整到F4中间表")
@RestController
public class ViewDLPurAdjPriceToF4Ctrl {

    @Reference(version = "1.0", group = "dl")
    ViewDLPurAdjPriceToF4QueryCmd viewDLPurAdjPriceToF4QueryCmd;

    @ApiOperation(value = "DL接口-采购价格调整FC->F4")
    @RequestMapping(path = "/fc/api/puradjpricetof4/query", method = RequestMethod.POST)
    public JSONObject tranPlanSave(HttpServletRequest request,
                                   @RequestParam(value = "param", required = true) String param) throws Exception {

        ValueHolder result = new ValueHolder();
        try {
            HashMap map = new HashMap(16);
            map.put("param", JSON.parseObject(param));
            Object o = ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(),
                    "com.jackrain.nea.fc.dl.api.ViewDLPurAdjPriceToF4QueryCmd",
                    "dl", "1.0");
            result = ((ViewDLPurAdjPriceToF4QueryCmd) o).execute(map);
            if (result == null) {
                result = new ValueHolder();
                result.put("code", -1);
                result.put("error", "服务错误：返回为空");
            }
            if (result.get("code") == null) {
                result.put("code", 0);
            }

        } catch (Exception e) {
            result.put("code", -1);
            if (e instanceof NDSException) {
                result.put("error", "服务错误：" + e.getMessage());
            } else {
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                e.printStackTrace(pw);
                result.put("error", "服务错误：" + sw.toString());
            }
        }

        return result.toJSONObject();
    }

}
